package cn.bsb.service.rece;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import cn.bsb.dao.ArticleMapper;
import cn.bsb.dao.BookMapper;
import cn.bsb.dao.CommentMapper;
import cn.bsb.dao.LikeMapper;
import cn.bsb.pojo.Article;
import cn.bsb.pojo.Bookmark;
import cn.bsb.pojo.Comment;
import cn.bsb.pojo.DevUser;
import cn.bsb.pojo.like;
import tk.mybatis.mapper.entity.Example;

@Service("ArticleService")
public class ArticleService extends BaseService<Article> {
	@Autowired
	private ArticleMapper articleMapper;
	@Autowired
	private LikeMapper likemapper;
	@Autowired
	private CommentMapper cmapper;
	@Autowired
	private BookMapper bookmapper;

	@Transactional(propagation=Propagation.SUPPORTS)
	public PageInfo<Article> getArticles(Integer pageNum,Integer pageSize,Article article){
		PageHelper.startPage(pageNum, pageSize);
		List<Article> list=null;
	  
	    if (article!=null&&article.getTitle()!=null&&!article.getTitle().equals("")) {
	    	list=articleMapper.getSelectList(article.getTitle());
		} else {
			Example example=new Example(Article.class);
			if(article!=null&&article.getThematic_id()!=0&&article.getThematic_id()!=0) {
				example.createCriteria().andCondition("thematic_id=", article.getThematic_id());
				
			}
		    example.setOrderByClause("creattime desc");
		    list=articleMapper.selectByExample(example);
		}
	   
		
		PageInfo<Article> page = new PageInfo<Article>(list);
		return page;
	}
	/**
	 * 查询热门的文章
	 * @param list
	 * @return
	 */
	public  List<Article> getRecomment(List list){
		List<Article> articlelist=null;
		Example example=new Example(Article.class);
	    example.setOrderByClause("creattime desc");
	    example.createCriteria().andIn("id", list);
	    articlelist=articleMapper.selectByExample(example);
	    return articlelist;
	}
	public PageInfo<Article> getList(Integer uid, int pageNum, int pageSize){
		PageHelper.startPage(pageNum, pageSize);
		List<Article> list = articleMapper.getArticles(uid);
		PageInfo<Article> page = new PageInfo<Article>(list);
		return page;
	}
	
	public int getCount(Integer id) {
		return articleMapper.getCount(id);
	}
	//查文章详情
	public Article getArticle(Long id) {
		
		
		return articleMapper.getArticle(id);
	}
	
	//添加文章
	@Transactional(propagation=Propagation.SUPPORTS)
	public int addArticle(Article ai) {
		return save(ai);
	}
	
	
	public int addLike(Long vid,Long aid) {
		return likemapper.addLike(vid, aid);
	}
	
	//评论列表
	public List<Comment> getComment(Long aid){
		return cmapper.getComment(aid);
	}
	
	//增加评论
	public int isComment(Comment comment) {
		return cmapper.addComment(comment);
	}
	
	//查看是否点过赞
	public like getLike(Long vid,Long aid) {
		return likemapper.getLike(vid, aid);
	}
	//数量增加
	public int like(Long id) {
		return articleMapper.like(id);
	}
	//收藏
	public int addBook(Bookmark book) {
		return bookmapper.addBook(book);
	}
	
	//是否收藏
	public Bookmark isBookmark(Long vid,Long aid) {
		return bookmapper.isBookmark(vid, aid);
	}
	//删除收藏
	public int deBookmark(Long vid,Long aid) {
		return bookmapper.deBookMark(vid, aid);
	}
	//查看收藏
	public List<Bookmark> getBookList(long id){
		return bookmapper.getBookList(id);
	}
	
	public int addlook(int id) {
		return articleMapper.addlook(id);
	}
	public DevUser getDevuserByAricleId(Long id){
		
		return articleMapper.getDevUserByarticleId(id);
	}
}
